package com.think.teaching.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.think.teaching.mapper.TrainingTestMapper;
import com.think.teaching.model.TrainingTest;
import com.think.teaching.service.TrainingTestService;
import org.springframework.stereotype.Service;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author think
 * @since 2020-08-12
 */
@Service
public class TrainingTestServiceImpl extends BaseServiceImpl<TrainingTestMapper, TrainingTest> implements TrainingTestService {

	@Override
	public TrainingTest AddTrainingTest(TrainingTest trainingTest) {
		if (this.save(trainingTest)) {
			return trainingTest;
		}
		return null;
	}

	@Override
	public TrainingTest UpdateTrainingTest(TrainingTest trainingTest) {
		if (this.updateById(trainingTest)) {
			return trainingTest;
		}
		return null;
	}

	@Override
	public TrainingTest GetTrainingTest(Integer id) {
		QueryWrapper<TrainingTest> queryWrapper = new QueryWrapper<>();
		queryWrapper.eq("id", id).eq("state", 1).ne("delflag", 1);
		return getOne(queryWrapper);
	}

	@Override
	public IPage<TrainingTest> ListTrainingTest(Integer page, Integer count, Boolean withoutBan) {
		QueryWrapper<TrainingTest> queryWrapper = new QueryWrapper<>();
		queryWrapper.ne("delflag", 1);
		if (withoutBan) {
			queryWrapper.eq("state", 1);
		}
		IPage<TrainingTest> ipage = new Page<>(page, count);
		return this.page(ipage);
	}

	@Override
	public TrainingTest SetTrainingTestState(Integer id,Integer state) {
		UpdateWrapper<TrainingTest> queryWrapper = new UpdateWrapper<>();
		queryWrapper.eq("id", id).ne("delflag", 1).set("state", state);
		this.update(queryWrapper);
		return getById(id);
	}

}
